/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package strategies;

import exceptions.DatabaseException;
import general.IContext;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import wrappers.EmployeeWrap;

/**
 *
 * @author KAV
 */
public class GroupEditRowsStrategy extends AbstractStrategy {

    @Override
    public void executeOperation(IContext context) {
        prepareContext(context);
        if (dao != null) {
            List<Integer> groupEditIds = new LinkedList<Integer>();
            Set<String> keys = pp.getParamMap().keySet();
            for (String s : keys) {
                if (s.startsWith("editrow_")) {
                    int newId = Integer.parseInt(s.replace("editrow_", ""));
                    groupEditIds.add(newId);
                }
            }
            for (int id : groupEditIds) {
                boolean res = true;
                String errorMessage = "";
                try {
                   EmployeeWrap empl = (EmployeeWrap) dao.findById(id);
                   EmployeeWrap emplNew = new EmployeeWrap(empl.getId(), empl.getName(), empl.getSurname(), empl.getJob(), empl.getManager(), empl.getDate(), empl.getSalary(), Integer.parseInt(pp.getParam("edit_department")));
                   dao.updateById(emplNew);
                } catch (DatabaseException de) {
                    res = false;
                    errorMessage = de.getMessage();
                }
                if (!res) {
                    op.putErrorReport("Row with id=" + id + " can't update.", errorMessage);
                } else {
                    op.putSuccessReport("Row with id=" + id + " updated");
                }
            }
        }
    }
}
